/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.model.beans;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author DeuterioX
 */
@Entity
@Table(name = "score")
@NamedQueries({
    @NamedQuery(name = "Score.findAll", query = "SELECT s FROM Score s")})
public class Score implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final int FORWARD_GOAL = 4;
    public static final int MIDFIELDER_GOAL = 5;
    public static final int DEFENDER_GOAL = 6;
    public static final int GOALKEEPER_GOAL = 8;
    public static final int OWN_GOAL = -2;
    public static final int RECEIVED_GOAL = -1;
    public static final int NOT_RECEIVED_GOALS = 2;
    public static final int CATCHED_PENALTY = 4;
    public static final int FAILED_PENALTY = -4;
    public static final int YELLOW_CARD = -2;
    public static final int RED_CARD = -4;
    public static final int FIGURE = 4;
    public static final int MIN_TIME_PLAYED = 20;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "catched_penalties", nullable=false)
    private int catchedPenalties;
    @Basic(optional = false)
    @Column(name = "failed_penalties", nullable=false)
    private int failedPenalties;
    @Basic(optional = false)
    @Column(name = "figure", nullable=false)
    private boolean figure;
    @Basic(optional = false)
    @Column(name = "goals", nullable=false)
    private int goals;
    @Basic(optional = false)
    @Column(name = "own_goals", nullable=false)
    private int ownGoals;
    @Basic(optional = false)
    @Column(name = "penalties", nullable=false)
    private int penalties;
    @Basic(optional = false)
    @Column(name = "received_goals", nullable=false)
    private int receivedGoals;
    @Basic(optional = false)
    @Column(name = "red_card", nullable=false)
    private boolean redCard;
    @Basic(optional = false)
    @Column(name = "score", nullable=false)
    private int score;
    @Basic(optional = false)
    @Column(name = "time", nullable=false)
    private int time;
    @Basic(optional = false)
    @Column(name = "yellow_card", nullable=false)
    private boolean yellowCard;
    @JoinColumn(name = "id_tournament_date", referencedColumnName = "id", nullable=false)
    @ManyToOne(optional = false)
    private TournamentDate tournamentDate;
    @JoinColumn(name = "id_soccer_player", referencedColumnName = "id", nullable=false)
    @ManyToOne(optional = false)
    private SoccerPlayer soccerPlayer;

    public Score() {
    }

    public Score(Integer id) {
        this.id = id;
    }

    public Score(Integer id, int catchedPenalties, int failedPenalties, boolean figure, int goals, int ownGoals, int penalties, int receivedGoals, boolean redCard, int score, int time, boolean yellowCard, TournamentDate tournamentDate, SoccerPlayer player) {
        this.id = id;
        this.catchedPenalties = catchedPenalties;
        this.failedPenalties = failedPenalties;
        this.figure = figure;
        this.goals = goals;
        this.ownGoals = ownGoals;
        this.penalties = penalties;
        this.receivedGoals = receivedGoals;
        this.redCard = redCard;
        this.score = score;
        this.time = time;
        this.yellowCard = yellowCard;
        this.tournamentDate = tournamentDate;
        this.soccerPlayer = player;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public int getCatchedPenalties() {
        return catchedPenalties;
    }

    public void setCatchedPenalties(int catchedPenalties) {
        this.catchedPenalties = catchedPenalties;
    }

    public int getFailedPenalties() {
        return failedPenalties;
    }

    public void setFailedPenalties(int failedPenalties) {
        this.failedPenalties = failedPenalties;
    }

    public boolean getFigure() {
        return figure;
    }

    public void setFigure(boolean figure) {
        this.figure = figure;
    }

    public int getGoals() {
        return goals;
    }

    public void setGoals(int goals) {
        this.goals = goals;
    }

    public int getOwnGoals() {
        return ownGoals;
    }

    public void setOwnGoals(int ownGoals) {
        this.ownGoals = ownGoals;
    }

    public int getPenalties() {
        return penalties;
    }

    public void setPenalties(int penalties) {
        this.penalties = penalties;
    }

    public int getReceivedGoals() {
        return receivedGoals;
    }

    public void setReceivedGoals(int receivedGoals) {
        this.receivedGoals = receivedGoals;
    }

    public boolean getRedCard() {
        return redCard;
    }

    public void setRedCard(boolean redCard) {
        this.redCard = redCard;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public boolean getYellowCard() {
        return yellowCard;
    }

    public void setYellowCard(boolean yellowCard) {
        this.yellowCard = yellowCard;
    }

    public TournamentDate getTournamentDate() {
        return tournamentDate;
    }

    public void setTournamentDate(TournamentDate tournamentDate) {
        this.tournamentDate = tournamentDate;
    }

    public SoccerPlayer getSoccerPlayer() {
        return soccerPlayer;
    }

    public void setSoccerPlayer(SoccerPlayer soccerPlayer) {
        this.soccerPlayer = soccerPlayer;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    public int getTotalScore() {
        int totalScore = 0;
        if( time >= MIN_TIME_PLAYED ) {
            totalScore = score + (failedPenalties * FAILED_PENALTY);
            if ( figure ) { totalScore = totalScore + FIGURE;  }
            if ( yellowCard ) { totalScore = totalScore + YELLOW_CARD; }
            if ( redCard ) { totalScore = totalScore + RED_CARD; }
            if ( soccerPlayer.getPosition().equals( SoccerPlayer.GOALKEEPER_POSITION ) ) {
                totalScore = totalScore + ( goals * GOALKEEPER_GOAL ) + ( receivedGoals * RECEIVED_GOAL ) + ( catchedPenalties * CATCHED_PENALTY );
                if ( receivedGoals == 0 ) { totalScore = totalScore + NOT_RECEIVED_GOALS; }
            } else if ( soccerPlayer.getPosition().equals( SoccerPlayer.DEFENDER_POSITION ) ) {
                totalScore = totalScore + ( goals * DEFENDER_GOAL ) + ( ownGoals * OWN_GOAL );
            } else if ( soccerPlayer.getPosition().equals( SoccerPlayer.MIDFIELDER_POSITION ) ) {
                totalScore = totalScore + ( goals * MIDFIELDER_GOAL ) + ( ownGoals * OWN_GOAL );
            } else if ( soccerPlayer.getPosition().equals( SoccerPlayer.FORWARD_POSITION ) ) {
                totalScore = totalScore + ( goals * FORWARD_GOAL ) + ( ownGoals * OWN_GOAL );
            } else {
                //Si no tiene una posición asiganada retorna 0.
                return 0;
            }
        }
        return totalScore;            
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Score)) {
            return false;
        }
        Score other = (Score) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "tpdisenio2009.model.beans.Score[id=" + id + "]";
    }

}
